package com.example.springboot014blogadmin.mapper;

import com.example.springboot014blogadmin.model.dto.ArticleDTO;
import com.example.springboot014blogadmin.po.ArticleDetailPO;
import com.example.springboot014blogadmin.po.ArticlePO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface ArticleMapper {
    @Select("""
            select id,user_id,title,summary,read_count,create_time,update_time
            from article
            where read_count >= #{lowRead}
            order by read_count desc
            limit #{topRead}
            """)
    @Results(id = "ArticleBaseMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "title", property = "title"),
            @Result(column = "summary", property = "summary"),
            @Result(column = "read_count", property = "readCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    List<ArticlePO> topSortByReadCount(Integer lowRead, Integer topRead);

    @Insert("""
            insert into
            article(user_id,title,summary,read_count,create_time,update_time)
            values(#{userId},#{title},#{summary},#{readCount},#{createTime},#{updateTime})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertArticle(ArticlePO articlePO);

    @Insert("""
            insert into article_detail(article_id, content)
            value (#{articleId},#{content})
            """)
    int insertArticleDetail(ArticleDetailPO articleDetailPO);

    @Select("""
            select m.id as articleId,title,summary,content
            from article m left join article_detail ad
            on m.id = ad.article_id
            where m.id = #{id}
            """)
    @Results({
            @Result(id = true, column = "articleId", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "summary", property = "summary"),
            @Result(column = "content", property = "content",
            jdbcType = JdbcType.LONGVARCHAR, javaType = String.class)
    })
    ArticleDTO selectArticleAndDetail(Integer id);

    // 更新文章title, summary
    @Update("""
            update article set title=#{title}, summary=#{summary} , update_time = #{updateTime} where id = #{id}
            """)
    int updateArticle(ArticlePO articlePO);

    @Update("""
            update article_detail set content=#{content} where article_id = #{articleId}
            """)
    int updateArticleDetail(ArticleDetailPO articleDetailPO);

    // 批量删除，动态sql
    @Delete("""
            <script>
            delete from article where id in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
            </script>
            """)
    int deleteArticle(List<Integer> ids);

    @Delete("""
            <script>
            delete from article_detail where id in 
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
            </script>
            """)
    int deleteArticleDetail(List<Integer> ids);

    @Select("""
            select id, article_id, content
            from article_detail
            where article_id = #{id}
            """)
    ArticleDetailPO selectDetailByArticleId(Integer id);
}
